/*
 * Copyright 2017 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yanzhenjie.sofia

import android.annotation.SuppressLint
import android.app.Activity
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.view.Window
import android.widget.FrameLayout
import android.widget.RelativeLayout
import androidx.annotation.ColorInt

/**
 * Created by YanZhenjie on 2017/8/30.
 */
@SuppressLint("ViewConstructor")
internal class HostLayout(private val mActivity: Activity) : RelativeLayout(
    mActivity
), Bar {

    companion object {
        private const val FLAG_NOT_INVASION = 0x0
        private const val FLAG_INVASION_STATUS = 0x1
        private const val FLAG_INVASION_NAVIGATION = 0x2
        private const val FLAG_INVASION_STATUS_AND_NAVIGATION =
            FLAG_INVASION_STATUS or FLAG_INVASION_NAVIGATION
    }

    private var mInvasionFlag = FLAG_NOT_INVASION

    private val mStatusView: StatusView?
        get() = findViewById(R.id.status_view)
    private val mNavigationView: NavigationView?
        get() = findViewById(R.id.navigation_view)
    private val mContentLayout: FrameLayout
        get() = findViewById(R.id.content)

    private fun replaceContentView() {
        val window = mActivity.window
        val contentLayout = window.decorView.findViewById<ViewGroup>(Window.ID_ANDROID_CONTENT)
        if (contentLayout.childCount > 0) {
            val contentView = contentLayout.getChildAt(0)
            contentLayout.removeView(contentView)
            val contentParams = contentView.layoutParams
            mContentLayout.addView(contentView, contentParams.width, contentParams.height)
        }
        contentLayout.addView(this, -1, -1)
    }

    override fun statusBarDarkFont(): Bar {
        Utils.setStatusBarDarkFont(mActivity, true)
        return this
    }

    override fun statusBarLightFont(): Bar {
        Utils.setStatusBarDarkFont(mActivity, false)
        return this
    }

    override fun statusBarBackground(@ColorInt statusBarColor: Int): Bar {
        mStatusView?.setBackgroundColor(statusBarColor)
        return this
    }

    override fun statusBarBackground(drawable: Drawable?): Bar {
        mStatusView?.background = drawable
        return this
    }

    override fun navigationBarDarkFont(): Bar {
        Utils.setLightNavigationBar(mActivity, true)
        return this
    }

    override fun navigationBarLightFont(): Bar {
        Utils.setLightNavigationBar(mActivity, false)
        return this
    }

    override fun navigationBarBackground(@ColorInt navigationBarColor: Int): Bar {
        mNavigationView?.setBackgroundColor(navigationBarColor)
        return this
    }

    override fun navigationBarBackground(drawable: Drawable?): Bar {
        mNavigationView?.background = drawable
        return this
    }

    override fun invasionStatusBar(): Bar {
        mInvasionFlag = mInvasionFlag or FLAG_INVASION_STATUS
        mStatusView?.invasionStatusBar()
        return this
    }

    override fun invasionNavigationBar(): Bar {
        mInvasionFlag = mInvasionFlag or FLAG_INVASION_NAVIGATION
        mNavigationView?.invasionNavigationBar()
        return this
    }

    private var orientation = mActivity.requestedOrientation

    override fun onConfigurationChanged(newConfig: Configuration?) {
        if (newConfig != null) {
            if (newConfig.orientation != orientation) {
                val statusBarColor = mStatusView?.background
                val navigationBarColor = mNavigationView?.background
                val contentLayout = mContentLayout
                val parent = parent as ViewGroup
                parent.removeView(this)
                removeAllViews()
                parent.addView(contentLayout)
                initialization()
                statusBarBackground(statusBarColor)
                navigationBarBackground(navigationBarColor)
                when (mInvasionFlag) {
                    FLAG_INVASION_STATUS -> {
                        invasionStatusBar()
                    }
                    FLAG_INVASION_NAVIGATION -> {
                        invasionNavigationBar()
                    }
                    FLAG_INVASION_STATUS_AND_NAVIGATION -> {
                        invasionStatusBar()
                        invasionNavigationBar()
                    }
                    FLAG_NOT_INVASION -> {
                    }
                    else -> {}
                }
                orientation = newConfig.orientation
            }
        }
        super.onConfigurationChanged(newConfig)
    }

    private fun initialization() {
        inflate(mActivity, R.layout.sofia_host_layout, this)
        replaceContentView()
        Utils.invasionStatusBar(mActivity)
        Utils.invasionNavigationBar(mActivity)
    }

    init {
        initialization()
    }
}